package org.tis.tools.abf.module.om.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tis.tools.abf.module.om.controller.request.OmBatchDeleteRequest;
import org.tis.tools.abf.module.om.dao.OmEmpGroupMapper;
import org.tis.tools.abf.module.om.entity.OmEmpGroup;
import org.tis.tools.abf.module.om.entity.OmGroup;
import org.tis.tools.abf.module.om.exception.OMExceptionCodes;
import org.tis.tools.abf.module.om.exception.OrgManagementException;
import org.tis.tools.abf.module.om.service.IOmEmpGroupService;
import org.tis.tools.abf.module.om.service.IOmGroupService;
import org.tis.tools.core.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

import static org.tis.tools.core.utils.BasicUtil.wrap;

/**
 * omEmpGroup的Service接口实现类
 * 
 * @author ljhuan
 * @date 2018/04/23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OmEmpGroupServiceImpl extends ServiceImpl<OmEmpGroupMapper, OmEmpGroup> implements IOmEmpGroupService {

    @Autowired
    IOmGroupService omGroupService;

    @Override
    public void insertGroupEmp(String groupCode, String empGuid) throws OrgManagementException {

        OmGroup omGroup = omGroupService.selectGroupByCode(groupCode);
        EntityWrapper<OmEmpGroup> omEmpGroupEntityWrapper = new EntityWrapper<>();
        omEmpGroupEntityWrapper.eq(OmEmpGroup.COLUMN_GUID_EMP, empGuid);
        omEmpGroupEntityWrapper.eq(OmEmpGroup.COLUMN_GUID_GROUP, omGroup.getGuid());

        List<OmEmpGroup> oegList = selectList(omEmpGroupEntityWrapper);
        if (oegList.size() > 0) {
            throw new OrgManagementException(OMExceptionCodes.IS_EXIST_BY_GROUP_CREAT_EMP, wrap(groupCode,empGuid));
        }
        OmEmpGroup oeg = new OmEmpGroup();
        oeg.setGuidEmp(empGuid);
        oeg.setGuidGroup(omGroup.getGuid());
        this.baseMapper.insert(oeg);
    }

    @Override
    public void deleteGroupEmp(String groupCode,String guidEmp) {

        OmGroup omGroup = omGroupService.queryGroupByCode(groupCode);
        EntityWrapper<OmEmpGroup> omEmpGroupEntityWrapper = new EntityWrapper<>();
        omEmpGroupEntityWrapper.eq(OmEmpGroup.COLUMN_GUID_GROUP,omGroup.getGuid());
        omEmpGroupEntityWrapper.eq(OmEmpGroup.COLUMN_GUID_EMP,guidEmp);

        this.baseMapper.delete(omEmpGroupEntityWrapper);
    }

    /**
     * 批量删除人员-工作组关系表数据
     */
    @Override
    public void batchDelete(OmBatchDeleteRequest deleteRequest) throws OrgManagementException {

        if (0 == deleteRequest.getDeleteList().size() || StringUtil.isEmpty(deleteRequest.getGuid())){
            throw new OrgManagementException(OMExceptionCodes.PARAMETER_IS_EMPTY_WHRN_BATCH_DELETE,wrap("删除时参数列表为空"));
        }

        List<OmEmpGroup> omEmpGroupList = new ArrayList<OmEmpGroup>();
        for (String empGuid : deleteRequest.getDeleteList()){
            if (!StringUtil.isEmpty(empGuid)){
                Wrapper<OmEmpGroup> omEmpGroupWrapper = new EntityWrapper<OmEmpGroup>();
                omEmpGroupWrapper.eq(OmEmpGroup.COLUMN_GUID_EMP,empGuid);
                omEmpGroupWrapper.eq(OmEmpGroup.COLUMN_GUID_GROUP,deleteRequest.getGuid());
                OmEmpGroup omEmpGroup = selectOne(omEmpGroupWrapper);
                if (null == omEmpGroup){
                    throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_QUERY_OM_GROUP_EMPLOYEE,wrap
                            ("查询的人员工作组关系不存在或已删除！"));
                }
                omEmpGroupList.add(omEmpGroup);
            }
        }

        for(OmEmpGroup omEmpGroup : omEmpGroupList){
            if (null != omEmpGroup){
                deleteById(omEmpGroup.getGuid());
            }
        }
    }
}

